//
//  ViewController.m
//  练习-相册
//
//  Created by Jorn.Wu on 15/7/13.
//  Copyright (c) 2015年 Jorn.Wu. All rights reserved.
//

#import "ViewController.h"
#import "ImageViewController.h"


@interface ViewController ()
@property (nonatomic, strong) NSMutableArray *imageBtnAr;
@property (nonatomic, strong) NSMutableArray *imageNameAr;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"相册";
    self.navigationController.toolbarHidden = NO;
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.toolbar.translucent = NO;
    
    [self addItems];
    [self addImages];
    
}
- (void)addItems{
    UIBarButtonItem *searchBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:nil];
    UIBarButtonItem *chooseBtn = [[UIBarButtonItem alloc] initWithTitle:@"选择" style:UIBarButtonItemStylePlain target:self action:nil];
    self.navigationItem.leftBarButtonItem = searchBtn;
    self.navigationItem.rightBarButtonItem = chooseBtn;
    
    UIBarButtonItem *organizeBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize target:self action:nil];
    UIBarButtonItem *flexibleSpaceBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIBarButtonItem *trashBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:nil];
    UIBarButtonItem *replyBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:nil];
    
    self.toolbarItems = @[replyBtn,flexibleSpaceBtn,trashBtn,flexibleSpaceBtn,organizeBtn];
    
    
    
}
- (void)addImages{
    
    NSMutableArray *imgAr = [NSMutableArray array];
    _imageNameAr = [NSMutableArray array];

    for (int i = 0; i < 10; i++) {
        NSString *str = [NSString stringWithFormat:@"huoying%d.jpg",i + 1];
        UIImage *img = [UIImage imageNamed:str];
        
        [imgAr addObject:img];
        [_imageNameAr addObject:str];
        
    }
    
    CGFloat width = 90;
    CGFloat height = 90;
    int num = 10;
    int col = 4;
    CGFloat gap = ([UIScreen mainScreen].bounds.size.width - width * col) / (col + 1);
    
    _imageBtnAr = [NSMutableArray array];
    
    for (int i = 0; i < num; i++) {
        int a = i % col;//第几列
        int b = i / col;//第几行
        
        UIButton *imgBtn = [[UIButton alloc] initWithFrame:CGRectMake((gap + width) * a + gap, b * (gap + height) + gap + 10, width, height)];
        [imgBtn setImage:imgAr[i] forState:UIControlStateNormal];
        imgBtn.tag = 100 + i;
        [imgBtn addTarget:self action:@selector(imageBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.view addSubview:imgBtn];
        
        [_imageBtnAr addObject:imgBtn];
    }
}
- (void)imageBtnAction:(UIButton *)button{
    ImageViewController *imgVC = [[ImageViewController alloc] init];
    imgVC.imageAr = _imageBtnAr;
    imgVC.clickTag = button.tag;
    imgVC.imageNameAr = _imageNameAr;
    
    [self.navigationController pushViewController:imgVC animated:YES];
}

@end
